package com.tiamo.traders.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tiamo.traders.entity.ResultModel;
import com.tiamo.traders.entity.User;
import com.tiamo.traders.service.UserSeervice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/tiamo/user")
public class UserController {

    @Autowired(required = true)
    private UserSeervice userSeervice;

    @Autowired
    ResultModel resultModel;


//    @RequestMapping("/findAll")
//    public ResultModel userFindAll(User user){
//        user.setPageNo((user.getPageNo()-1)*user.getPageSize());
//        return ResultModel.getResultModel(userSeervice.userFindAll(user));
//    }
//
//    @RequestMapping("getCount")
//    public ResultModel userGetCount(User user){
//        return ResultModel.getResultModel(userSeervice.userGetCount(user));
//    }
//
//    @RequestMapping("/add")
//    public ResultModel userAdd(User user, @RequestParam String address1, @RequestParam String address2,  @RequestParam String address3){
//        user.setAddress(address1+" "+address2+" "+address3);
//        return ResultModel.getResultModel(userSeervice.userAdd(user));
//    }
//
//    @RequestMapping("/updateEnable")
//    public ResultModel userUpdateEnable(User user){
//        return ResultModel.getResultModel(userSeervice.userUpdateEnable(user));
//    }
//
//    @RequestMapping("/delete")
//    public ResultModel userDelete(Integer id){
//        return ResultModel.getResultModel(userSeervice.userDeleteById(id));
//    }
//
//    @RequestMapping("/findById")
//    public ResultModel userFindById(Integer id) {
//        return ResultModel.getResultModel(userSeervice.userFindById(id));
//    }
//
//    @RequestMapping("/update")
//    public ResultModel userUpdate(User user) {
//        return ResultModel.getResultModel(userSeervice.userUpdate(user));
//    }

    @RequestMapping("/findAll")
    public PageInfo<User> userFindAll(User user){
        PageHelper.startPage(user.getPageNo(),user.getPageSize());
//        user.setPageNo((user.getPageNo()-1)*user.getPageSize());
        PageInfo<User> pageInfo=new PageInfo<User>(userSeervice.userFindAll(user));
        return pageInfo;
    }

    @RequestMapping("getCount")
    public Integer userGetCount(User user){
        return userSeervice.userGetCount(user);
    }
    /*
     * (value="brithday", required=true)
     * */
    @RequestMapping("/add")
    public Integer userAdd(User user, @RequestParam String address1, @RequestParam String address2,  @RequestParam String address3){
        user.setAddress(address1+" "+address2+" "+address3);
        return userSeervice.userAdd(user);
    }

    @RequestMapping("/updateEnable")
    public Integer userUpdateEnable(User user){
        return userSeervice.userUpdateEnable(user);
    }


    @RequestMapping("/delete")
    public Integer userDelete(Integer id){

        return userSeervice.userDeleteById(id);
    }

    @RequestMapping("/findById")
    public User userFindById(Integer id) {

        return userSeervice.userFindById(id);
    }

    @RequestMapping("/update")
    public Integer userUpdate(User user) {
        Integer res = userSeervice.userUpdate(user);
        return res;
    }
}
